package com.study.mynew.ui.Fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.study.mynew.R;
import com.study.mynew.adapter.NewItemAdapter;
import com.study.mynew.api.Apiclient;
import com.study.mynew.bean.NewItemBean;
import com.study.mynew.bean.NewsData;
import com.study.mynew.ui.NewsActivity;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link NewsFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class NewsFragment extends BaseFragment {
    public static String EXTRA_CATEGORY = "EXTRA_CATEGORY";
    private NewItemAdapter mAdapter ;

    private RecyclerView recyclerView;
    private RefreshLayout refreshLayout;

    private  HashMap<String,String> keyMap = new HashMap<>();
    private  String key = "";
    public NewsFragment() {
        // Required empty public constructor
        keyMap.put("推荐","top");
        keyMap.put("国内","guonei");
        keyMap.put("国际","guoji");
        keyMap.put("娱乐","yule");
        keyMap.put("体育","tiyu");
        keyMap.put("军事","junshi");
        keyMap.put("科技","keji");
        keyMap.put("财经","caijing");
        keyMap.put("游戏","youxi");
        keyMap.put("汽车","qiche");
        keyMap.put("健康","jiankang");
    }

    public static NewsFragment newInstance(String category) {
        NewsFragment fragment = new NewsFragment();
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_CATEGORY,category);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_news, container, false);
        recyclerView = v.findViewById(R.id.RecyclerView);
        refreshLayout = v.findViewById(R.id.refreshLayout);
        key = keyMap.get(getArguments().getString(EXTRA_CATEGORY));
        return v;
    }

    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new NewItemAdapter();
        recyclerView.setAdapter(mAdapter);
        refreshLayout.autoRefresh(0,250,1.0f,true);
        getNewsList(1,key);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                NewItemBean item = mAdapter.getItem(position);
                Intent intent = new Intent(getContext(), NewsActivity.class);
                intent.putExtra(NewsActivity.EXTRA_DATA, item);
                startActivity(intent);
            }
        });


//        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(RefreshLayout refreshlayout) {
//                refreshlayout.finishRefresh(9000);
//                getNewsList(1,key);
//            }
//        });



    }

    private void getNewsList(int page, String categories) {
        Apiclient.getInstance().getApi().getNewsItem("0451fc3015ff765dd674b138988d2c8a",categories,page,15,1).enqueue(new Callback<NewsData>() {
            @Override
            public void onResponse(Call<NewsData> call, Response<NewsData> response) {
                if(response.body().getError_code()==0){
                    mAdapter.setNewInstance(response.body().getResult().getData());
                }else if (response.body().getError_code() == 10012){
                    Toast.makeText(getContext(),"请求次数用完",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<NewsData> call, Throwable throwable) {
                Toast.makeText(getActivity(), "请求错误", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private String getDate(long timeMills) {
        //获取时间
        SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); //设置时间格式
        Date curDate = new Date(timeMills); //获取当前时间
        String createDate = formatter.format(curDate);
        return createDate;
    }
}